define(['jquery', 'bootstrap', 'backend', 'table', 'form'], function ($, undefined, Backend, Table, Form) {

    var Controller = {
        index: function () {
            // 初始化表格参数配置
            Table.api.init({
                extend: {
                    index_url: 'manystore/index/index',
                    add_url: 'manystore/index/add',
                    edit_url: 'manystore/index/edit',
                    del_url: 'manystore/index/del',
                    multi_url: 'manystore/index/multi',
                }
            });

            var table = $("#table");

            // 初始化表格
            table.bootstrapTable({
                url: $.fn.bootstrapTable.defaults.extend.index_url,
                columns: [
                    [
                        {field: 'state', checkbox: true, },
                        {field: 'id', title: 'ID'},
                        {field: 'username', title: __('Username')},
                        {field: 'nickname', title: __('Nickname')},
                        {field: 'email', title: __('Email')},
                        {field: 'status', title: __("Status"), formatter: Table.api.formatter.status},
                        {field: 'logintime', title: __('Login time'), formatter: Table.api.formatter.datetime, operate: 'RANGE', addclass: 'datetimerange', sortable: true},
                        {
                            field: 'operate',
                            title: __('Operate'),
                            table: table,
                            events: Table.api.events.operate,
                            formatter: Table.api.formatter.operate,
                            buttons: [
                                {
                                    name: 'banji',
                                    text: __('班级列表'),
                                    title: __('班级列表'),
                                    classname: 'btn btn-xs btn-success btn-primary btn-dialog btn-area-big',
                                    extend: 'data-area=\'["95%", "95%"]\'',
                                    icon: 'fa',
                                    url: function(row){
                                        let _url = Config.moduleurl + '/manystore/banji?shop_id='+row.id;
                                        // let _url = Config.moduleurl + '/myadmin/camera?company_id='+row.id;
                                        return _url;
                                    },
                                    visible: function (row) {
                                        return true;
                                    }
                                },
                                {
                                    name: 'banji',
                                    text: __('监控平台'),
                                    title: __('监控平台'),
                                    classname: 'btn btn-xs btn-success btn-primary btn-dialog btn-area-big',
                                    extend: 'data-area=\'["95%", "95%"]\'',
                                    icon: 'fa',
                                    url: function(row){
                                        let _url = Config.moduleurl + '/manystore/monitor?shop_id='+row.id;
                                        // let _url = Config.moduleurl + '/myadmin/camera?company_id='+row.id;
                                        return _url;
                                    },
                                    visible: function (row) {
                                        return true;
                                    }
                                },
                                // {
                                //     name: 'banji',
                                //     text: __('老师列表'),
                                //     title: __('老师列表'),
                                //     classname: 'btn btn-xs btn-success btn-primary btn-dialog btn-area-big',
                                //     extend: 'data-area=\'["95%", "95%"]\'',
                                //     icon: 'fa',
                                //     url: function(row){
                                //         let _url = Config.moduleurl + '/manystore/teacher?shop_id='+row.id;
                                //         // let _url = Config.moduleurl + '/myadmin/camera?company_id='+row.id;
                                //         return _url;
                                //     },
                                //     visible: function (row) {
                                //         return true;
                                //     }
                                // },
                                // {
                                //     name: 'banji',
                                //     text: __('家长列表'),
                                //     title: __('家长列表'),
                                //     classname: 'btn btn-xs btn-success btn-primary btn-dialog btn-area-big',
                                //     extend: 'data-area=\'["95%", "95%"]\'',
                                //     icon: 'fa',
                                //     url: function(row){
                                //         let _url = Config.moduleurl + '/manystore/parents?shop_id='+row.id;
                                //         // let _url = Config.moduleurl + '/myadmin/camera?company_id='+row.id;
                                //         return _url;
                                //     },
                                //     visible: function (row) {
                                //         return true;
                                //     }
                                // },
                                // {
                                //     name: 'banji',
                                //     text: __('学生列表'),
                                //     title: __('学生列表'),
                                //     classname: 'btn btn-xs btn-success btn-primary btn-dialog btn-area-big',
                                //     extend: 'data-area=\'["95%", "95%"]\'',
                                //     icon: 'fa',
                                //     url: function(row){
                                //         let _url = Config.moduleurl + '/manystore/student/index?shop_id='+row.id;
                                //         return _url;
                                //     },
                                //     visible: function (row) {
                                //         return true;
                                //     }
                                // },
                            ]
                        }
                    ]
                ]
            });

            // 为表格绑定事件
            Table.api.bindevent(table);
        },
        add: function () {
            $("#c-address_city").on("cp:updated", function() {
                var citypicker = $(this).data("citypicker");
                var province = citypicker.getCode("province");
                var city = citypicker.getCode("city");
                var district = citypicker.getCode("district");
                if(province){
                    $("#province").val(province);
                }
                if(city){
                    $("#city").val(city);
                }
                if(district){
                    $("#district").val(district);
                }
                $(this).blur();
            });
            Form.api.bindevent($("form[role=form]"));
        },
        edit: function () {
            $("#c-address_city").on("cp:updated", function() {
                var citypicker = $(this).data("citypicker");
                var province = citypicker.getCode("province");
                var city = citypicker.getCode("city");
                var district = citypicker.getCode("district");
                if(province){
                    $("#province").val(province);
                }
                if(city){
                    $("#city").val(city);
                }
                if(district){
                    $("#district").val(district);
                }
            });
            Form.api.bindevent($("form[role=form]"));
        }
    };
    return Controller;
});
